package cn.ncist.health_backend.controller;

import cn.ncist.common.RespResult;
import cn.ncist.constant.MessageConstant;
import cn.ncist.dto.ReportData;
import cn.ncist.health_interface.service.MemberService;
import cn.ncist.health_interface.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/report")
public class ReportController {

    @DubboReference
    private MemberService memberService;

    @DubboReference
    private ReportService reportService;

    //会员数量折线图数据
    @GetMapping("/getMemberReport")
    public RespResult getMemberReport() {
        Map<String, Object> map = new HashMap<>();
        List<String> months = new ArrayList();
        Calendar calendar = Calendar.getInstance();//获得日历对象，模拟时间就是当前时间
        //计算过去一年的12个月
        calendar.add(Calendar.MONTH, -12);//获得当前时间往前推12个月的时间
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH, 1);//获得当前时间往后推一个月日期
            Date date = calendar.getTime();
            months.add(new SimpleDateFormat("yyyy.MM").format(date));
        }
        map.put("months", months);

        List<Integer> memberCount = memberService.findMemberCountByMonths(months);
        map.put("memberCount", memberCount);
        return new RespResult(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
    }

    //套餐预约占比饼形图
    @GetMapping("/getSetmealReport")
    public RespResult getSetMealReport(){
        try {
            List<Map<String, Object>> setmealList = memberService.findSetmealCount();
            Map<Object, Object> map = new HashMap<>();
            map.put("setmealCount",setmealList);
            List<String> setmealNameList = new ArrayList<>();
            for (Map<String, Object> setmealMap : setmealList) {
                setmealNameList.add((String) setmealMap.get("name"));
            }
            map.put("setmealNames",setmealNameList);
            log.info("返回给前端的数据为：{}",map.toString());
            return new RespResult(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
            return new RespResult(false,MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }

    @RequestMapping("/exportBusinessReport")
    public RespResult exportBusinessReport(HttpServletRequest request, HttpServletResponse response){

        return null;
    }


    @GetMapping("/getBusinessReportData")
    public RespResult getBusinessReportData(){
        ReportData reportData = null;
        try {
            reportData = reportService.exportBusinessReport(new Date());
            log.info("运营数据为：{}",reportData.toString());
            return new RespResult(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS,reportData);
        } catch (Exception e) {
            e.printStackTrace();
            return new RespResult(false,MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }
}
